from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, DestroyModelMixin, \
    UpdateModelMixin

from utils.common_response import APIResponse
from django.core.cache import cache
from rest_framework.exceptions import APIException

from utils.decorators import wrap_cache


class APIListModelMixin(ListModelMixin):
    cache_key = None

    def list(self, request, *args, **kwargs):
        # assert self.cache_key, APIException('必须指定类属性cache_key')
        res = super().list(request, *args, **kwargs)
        results = res.data
        if self.paginator:
            return APIResponse(**res.data)
        return APIResponse(results=results)


class APIRetrieveModelMixin(RetrieveModelMixin):
    def retrieve(self, request, *args, **kwargs):
        res = super().retrieve(request, *args, **kwargs)
        return APIResponse(result=res.data)


class APICreateModelMixin(CreateModelMixin):
    def create(self, request, *args, **kwargs):
        res = super().create(request, *args, **kwargs)
        return APIResponse(result=res.data)


class APIUpdateModelMixin(UpdateModelMixin):
    def update(self, request, *args, **kwargs):
        res = super().update(request, *args, **kwargs)
        return APIResponse(result=res.data)


class APIDestroyModelMixin(DestroyModelMixin):
    def destroy(self, request, *args, **kwargs):
        res = super().destroy(request, *args, **kwargs)
        return APIResponse(result=res.data)
